package cn.ztr.ymcc.service.impl;

import cn.ztr.ymcc.domain.Login;
import cn.ztr.ymcc.domain.User;
import cn.ztr.ymcc.domain.UserBaseInfo;
import cn.ztr.ymcc.domain.UserRealInfo;
import cn.ztr.ymcc.dto.UserRegisterDTO;
import cn.ztr.ymcc.feign.UaaFeign;
import cn.ztr.ymcc.mapper.UserBaseInfoMapper;
import cn.ztr.ymcc.mapper.UserMapper;
import cn.ztr.ymcc.mapper.UserRealInfoMapper;
import cn.ztr.ymcc.result.JsonResult;
import cn.ztr.ymcc.service.IUserService;
import cn.ztr.ymcc.util.AssertUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.seata.spring.annotation.GlobalTransactional;
import org.bouncycastle.jcajce.provider.asymmetric.rsa.BCRSAPrivateCrtKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员登录账号 服务实现类
 * </p>
 *
 * @author ztr
 * @since 2023-02-26
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UaaFeign uaaFeign;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserRealInfoMapper userRealInfoMapper;
    @Autowired
    private UserBaseInfoMapper userBaseInfoMapper;

    /**
     * 根据手机号查找用户
     * @param phone
     * @return
     */
    @Override
    public JsonResult findByPhone(String phone) {
        User user = baseMapper.selectOne(new QueryWrapper<User>().eq("phone", phone));
        return JsonResult.me(user);
    }

    /**
     * 用户注册
     * @param userRegisterDTO
     * @return
     */
    @Override
    @GlobalTransactional(rollbackFor = Exception.class)
    public JsonResult register(UserRegisterDTO userRegisterDTO) {
        //1 手机验证码校验
        AssertUtil.isNotNull(redisTemplate.hasKey(userRegisterDTO.getMobile()),"请先获取验证码");
        String o = (String) redisTemplate.opsForValue().get(userRegisterDTO.getMobile());
        AssertUtil.isTrue(o.equals(userRegisterDTO.getSmsCode()),"验证码错误");
        //2 对密码进行加密
        String encode = new BCryptPasswordEncoder().encode(userRegisterDTO.getPassword());
        //3 保存login登录信息
        Login login = new Login().setUsername(userRegisterDTO.getMobile()).setPassword(encode);
        JsonResult jsonResult = uaaFeign.saveOrUpdate(login);
        String s = JSON.toJSON(jsonResult.getData()).toString();
        Login loginResult = JSON.parseObject(s, Login.class);
        Long loginResultId = loginResult.getId();
        //4 保存user注册信息
        User user = new User().setLoginId(loginResultId)
                                .setPhone(userRegisterDTO.getMobile())
                                .setNickName(userRegisterDTO.getMobile());
        userMapper.insert(user);
        //5 保存user-base-info信息
        UserBaseInfo userBaseInfo = new UserBaseInfo()
                .setRegChannel(userRegisterDTO.getRegChannel())
                .setUserId(user.getId());
        userBaseInfoMapper.insert(userBaseInfo);
        //保存实名认证信息
        UserRealInfo userRealInfo = new UserRealInfo().setUserId(user.getId());
        userRealInfoMapper.insert(userRealInfo);
        //删除redis中的验证码
        redisTemplate.delete(userRegisterDTO.getMobile());
        return JsonResult.me();
    }
}
